package com.bluesky.javawebbrowser.domain.html.tags.form.select;

import java.util.ArrayList;
import java.util.List;

import com.bluesky.common.TreeNode;
import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.javawebbrowser.domain.html.tags.TagType;
import com.bluesky.javawebbrowser.domain.html.tags.form.FormElement;

public class Select extends Tag implements FormElement {

	private String name;

	private String onChange;

	public Integer getSize() {
		String v = getAttribute("size");
		if (v != null)
			return Integer.valueOf(v);
		else
			return null;
	}

	public void setSize(Integer size) {
		if (size == null)
			removeAttribute("size");
		else
			setAttribute("size", size.toString());
	}

	public boolean isMultiple() {

		return attributes.containsKey("multiple");

	}

	public void setMultiple(boolean b) {
		if (b)
			setAttribute("multiple", null);
		else
			removeAttribute("multiple");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;

		attributes.put("name", name);
	}

	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		this.onChange = onChange;

		setAttribute("onchange", onChange);
	}

	public Select() {
		super(TagType.SELECT);
	}

	public Select(Tag tag) {
		super(TagType.SELECT);

		copy(tag);
	}

	@Override
	public String getHttpValue() {

		return getSelectedValue().toString();
	}

	@Override
	public void setHttpValue(String value) {
		String[] values = value.split(",");
		List<String> list = new ArrayList<String>();
		for (String s : values)
			list.add(s);

		for (TreeNode tn : getChildren()) {
			Option o = (Option) tn;

			int index = list.indexOf(o.getValue());
			if (index >= 0)
				o.setSelected(true);
			else
				o.setSelected(false);

		}

	}

	public int getSelectedIndex() {
		if (isMultiple())
			throw new RuntimeException("it is in multiple mode.");

		for (int i = 0; i < getChildren().size(); i++) {
			TreeNode tn = getChildren().get(i);

			Option o = (Option) tn;
			if (o.getSelected())
				return i;
		}
		return -1;
	}

	public int[] getSelectedIndices() {
		if (!isMultiple())
			throw new RuntimeException("it is not in multiple mode.");

		List<Integer> result = new ArrayList<Integer>();

		for (int i = 0; i < getChildren().size(); i++) {
			TreeNode tn = getChildren().get(i);

			Option o = (Option) tn;
			if (o.getSelected())
				result.add(i);
		}

		int[] array = new int[result.size()];
		for (int i = 0; i < result.size(); i++)
			array[i] = result.get(i);

		return array;
	}

	public void setSelectedIndex(int index) {
		if (isMultiple())
			throw new RuntimeException("it is in multiple mode.");

		for (int i = 0; i < getChildren().size(); i++) {
			TreeNode tn = getChildren().get(i);

			Option o = (Option) tn;
			if (i == index)
				o.setSelected(true);
			else
				o.setSelected(false);
		}
	}

	public void setSelectedIndices(int[] indices) {
		if (!isMultiple())
			throw new RuntimeException("it is not in multiple mode.");

		List<Integer> indexList = new ArrayList<Integer>();
		for (int k = 0; k < indices.length; k++)
			indexList.add(new Integer(indices[k]));

		for (int i = 0; i < getChildren().size(); i++) {
			TreeNode tn = getChildren().get(i);

			Option o = (Option) tn;
			if (indexList.indexOf(i) >= 0)
				o.setSelected(true);
			else
				o.setSelected(false);
		}
	}

	public String getSelectedValue() {
		for (int i = 0; i < getChildren().size(); i++) {
			TreeNode tn = getChildren().get(i);

			Option o = (Option) tn;
			if (o.getSelected())
				return o.getValue();
		}
		return null;
	}

	protected int getIndexByValue(String value) {
		for (int i = 0; i < getChildren().size(); i++) {
			TreeNode tn = getChildren().get(i);

			Option o = (Option) tn;
			if (o.getValue().equals(value))
				return i;
		}
		return -1;

	}

	public void addOption(Option option) {
		addChild(option);
	}

	public void addOption(String value, String label) {
		Option op = new Option();
		op.setValue(value);
		op.setLabel(label);

		addOption(op);
	}

	/**
	 * clear all options
	 */
	public void clear() {
		removeAllChildren();
	}

}
